<?php

namespace app\admin\controller;

use app\admin\model\admin\I18n as I18nModel;
use app\admin\service\admin\ConfigService;
use app\admin\service\admin\DictService;
use app\admin\service\admin\I18nService;
use app\admin\service\admin\UserService;
use app\common\service\CaptchaService;
use app\admin\service\common\token\TokenServiceFactory;
use app\admin\service\common\token\TokenServiceInterface;
use app\admin\service\common\Tools;
use app\admin\service\dev\FieldService;
use app\common\Constants;
use app\common\ErrorCode;
use app\common\exception\BusinessException;
use app\common\facade\HttpHelper;
use app\common\facade\Principal;
use think\App;
use think\facade\Cache;
use think\facade\Log;
use utils\crypt\Crypt;

class Login extends Admin
{
    protected TokenServiceInterface $jwtTokenService;

    public function __construct(
        App                     $app,
        protected UserService   $userService,
        protected ConfigService $configService,
    )
    {
        parent::__construct($app);
        $this->jwtTokenService = TokenServiceFactory::create();
    }

    public function index()
    {
        return json(['message' => '这是登录接口']);
    }

    /**
     * 登录
     *
     * @return \think\response\Json
     */
    public function login(): \think\response\Json
    {
        $data = $this->request->only(['username', 'password', 'captchaCode', 'captchaKey'], 'post', null);
        // 如果开启验证码则检查验证码
        if (config('rds.captcha')) {
            CaptchaService::checkCaptcha($data);
        }
        // 检查用户
        $userInfo = $this->userService->checkUser($data);
        // 生成 Token
        $token = $this->jwtTokenService->generateToken($userInfo, 'access');
        // 记录登录日志
        event('AdminLog', ['type' => Constants::LOG_TYPE_LOGIN]);
        // 返回结果
        return $this->success(['data' => $token]);
    }

    /**
     * 登出
     *
     * @return \think\response\Json
     * @throws \Exception
     */
    public function logout(): \think\response\Json
    {
        // 获取 Token
        $token = HttpHelper::getBearerToken();
        // 记录登录日志
        event('AdminLog', ['type' => Constants::LOG_TYPE_LOGIN]);
        // 执行登出
        $this->jwtTokenService->deleteToken($token, 'access');
        // 登出
        return $this->success();
    }


    /**
     * 获取验证码
     *
     * @return \think\response\Json
     */
    public function captcha()
    {
        if (config('rds.captcha')) {
            $data = CaptchaService::getCaptcha();
        }
        $data['captchaShow'] = config('rds.captcha');
        return $this->success(["data" => $data]);
    }

    /**
     * 获取用户信息
     *
     * @return \think\response\Json
     */
    public function user(): \think\response\Json
    {
        // 用户完整信息
        $userInfo = $this->userService->getPrincipalInfo(Principal::getUserId());
        return $this->success(['data' => $userInfo]);
    }

    /**
     * 获取通用信息
     *
     * @return \think\response\Json
     */
    public function general(): \think\response\Json
    {
        $data = ConfigService::getConfigArray();
        // $data['captcha'] = config('rds.captcha');
        return $this->success(['data' => $data]);
    }

    /**
     * 获取公钥
     *
     * @return \think\response\Json
     */
    public function crypt(): \think\response\Json
    {
        $public_key = Crypt::getPublicKey();
        return $this->success(['data' => $public_key]);
    }

    /**
     * 获得菜单
     *
     * @return \think\response\Json
     * @throws \Exception
     */
    public function menus(): \think\response\Json
    {
        $menu_tree = $this->userService->getUserMenus(Principal::getUserId());
        return $this->success(['data' => $menu_tree]);
    }

    /**
     * 刷新 Token
     *
     * @return \think\response\Json
     */
    public function refresh(): \think\response\Json
    {
        $refresh_token = $this->request->post('refresh_token');
        $tokenInfo = $this->jwtTokenService->refreshToken($refresh_token);
        return $this->success(['data' => $tokenInfo]);
    }

    /**
     * 心跳
     *
     * @return \think\response\Json
     */
    public function heartbeat(): \think\response\Json
    {
        $this->jwtTokenService->updateOnline(HttpHelper::getBearerToken());
        return $this->success();
    }
}